package com.wzz.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzz.Util.createVerificationCode;
import com.wzz.entity.Answer;
import com.wzz.entity.Question;
import com.wzz.service.impl.*;
import com.wzz.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/util")
@Api(tags = "工具类接口")
public class UtilController {

    @Autowired
    private QuestionServiceImpl questionService;

    @Autowired
    private AnswerServiceImpl answerService;

    static String CODE;

    /**
     * 生成随机验证码图片
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/getCodeImg")
    @ApiOperation(value = "获取验证码图片流")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "帮助前端生成验证码", required = false, dataType = "string", paramType = "query")
    })
    public void getIdentifyImage(@RequestParam(required = false) String id, HttpServletResponse response) throws IOException {
        log.info("执行了===>UtilController中的getIdentifyImage方法");
        //设置不缓存图片
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        //指定生成的响应图片
        response.setContentType("image/jpeg");
        createVerificationCode code = new createVerificationCode();
        BufferedImage image = code.getIdentifyImg();
        code.getG().dispose();
        //将图形验证码IO流传输至前端
        ImageIO.write(image, "JPEG", response.getOutputStream());
        CODE = code.getCode();
    }

    @GetMapping("/getCode")
    @ApiOperation(value = "获取验证码")
    public CommonResult<String> getCode() {
        return new CommonResult<>(200, CODE);
    }


    @GetMapping("/importExcel")
    @ApiOperation(value = "importExcel")
    public void importExcel(@RequestParam(required = false) String id, HttpServletResponse response) throws IOException {
        log.info("执行了===>UtilController中的getIdentifyImage方法");
        //设置不缓存图片
        ExcelReader reader = ExcelUtil.getReader("d:/aaa.xls");
        List<List<Object>> readAll = reader.read();
        for (int i=1;i<readAll.size();i++){
//            System.out.print(readAll.get(i));
//            System.out.print("=========\n");
//            for (int j=0;j<readAll.get(i).size();j++){
//                System.out.print(readAll.get(i).get(j));
//                System.out.print("==============================================\n");
//
//
//            }

            String questionId=readAll.get(i).get(0).toString();
            String type=readAll.get(i).get(1).toString();
            String quContent=readAll.get(i).get(2).toString();
            //String allOption=readAll.get(i).get(0).toString();

            String D=readAll.get(i).get(3).toString();
            String E=readAll.get(i).get(4).toString();
            String F="";
            String G="";
            String H="";
            String I="";
            if(!type.equals("判断")){
                 F=readAll.get(i).get(5).toString();
                 G=readAll.get(i).get(6).toString();
                 H=readAll.get(i).get(7).toString();
                 I=readAll.get(i).get(8).toString();
            }

            String trueOptionStr=readAll.get(i).get(9).toString();
            String orderId=readAll.get(i).get(0).toString();

            String allOption="";
            String trueOption="";
            if(type.equals("单选")||type.equals("判断")||type.equals("多选")){
                Question insertQuestion= new Question();
                if(type.equals("单选")){
                    insertQuestion.setQuType(1);
                }
                if(type.equals("判断")){
                    insertQuestion.setQuType(3);
                }
                if(type.equals("多选")){
                    insertQuestion.setQuType(2);
                }
                List<Question> qus = questionService.list(new QueryWrapper<>());
                Integer currentQuId = qus.get(qus.size() - 1).getId() + 1;
                insertQuestion.setId(currentQuId);
                insertQuestion.setQuContent(quContent);
                insertQuestion.setQuBankName("保卫部安全考试");
                insertQuestion.setQuBankId("7");
                insertQuestion.setLevel(1);
                insertQuestion.setQuContent(quContent);
                insertQuestion.setCreateTime(new DateTime());
                insertQuestion.setCreatePerson("administrator");

                questionService.save(insertQuestion);
                Question question=questionService.getOne(new QueryWrapper<Question>().eq("qu_content", quContent));
                System.out.print(question);
                Answer answer=new Answer();
                answer.setQuestionId(question.getId());
                answer.setAnalysis(question.getAnalysis());
                if(D.length()!=0){
                    allOption+=D+"|";
                }
                if(E.length()!=0){
                    allOption+=E+"|";
                }
                if(F.length()!=0){
                    allOption+=F+"|";
                }
                if(G.length()!=0){
                    allOption+=G+"|";
                }
                if(H.length()!=0){
                    allOption+=H+"|";
                }
                if(I.length()!=0){
                    allOption+=I+"|";
                }
                allOption=allOption.substring(0, allOption.length() - 1);
                answer.setAllOption(allOption);

                String[] s = trueOptionStr.split("");
                for(String each : s) {
                    System.out.println(each);
                    if(each.equals("A")){
                        trueOption+="0|";
                    }
                    if(each.equals("B")){
                        trueOption+="1|";
                    }
                    if(each.equals("C")){
                        trueOption+="2|";
                    }
                    if(each.equals("D")){
                        trueOption+="3|";
                    }
                }
                System.out.print("==========trueOption");
                System.out.print(trueOption);
                trueOption=trueOption.substring(0, trueOption.length() - 1);
                answer.setTrueOption(trueOption);
                answerService.save(answer);
            }
        }
    }

}
